local suchou = fk.CreateSkill{
  name = "rfenghou_3d15j__suchou",
}

suchou:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@rfenghou_3d15j__suchou", 0)
end)

suchou:addEffect(fk.DamageCaused, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(suchou.name) or target ~= player then return false end
    local mark = player:getMark("rfenghou_3d15j__suchou_tar")
    return data.to.id == mark and data.damage < (data.to.hp + data.to.shield)
    and table.contains(player.player_skills, suchou.name) -- 防止虚拟技能没法删除
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    return room:askToSkillInvoke(player, {
      skill_name = suchou.name,
      prompt = "#rfenghou_3d15j__suchou-damage:"..data.to.id.."::"..((data.to.hp + data.to.shield))
    })
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    data.damage = (data.to.hp + data.to.shield)
    room:handleAddLoseSkills(player, "-"..suchou.name)
  end,
})

suchou:addEffect(fk.Damage, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(suchou.name) or target ~= player then return false end
    local mark = player:getMark("rfenghou_3d15j__suchou_tar")
    return data.to.id == mark and player:isWounded()
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:recover {
      num = 1,
      skillName = suchou.name,
      who = player,
      recoverBy = player
    }
  end,

  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(suchou.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local currentDamage = room.logic:getCurrentEvent():findParent(GameEvent.Damage, true)
    if not currentDamage then return end
    local lastDamage = room.logic:getActualDamageEvents(1, function(e)
      return e.data.from == player and e.id < currentDamage.id
    end, nil, 1)[1]
    if lastDamage then
      local mark = player:getMark("rfenghou_3d15j__suchou_tar")
      local new = 0
      if lastDamage.data.to == data.to then
        new = data.to.id
      end
      if mark ~= new then
        room:doIndicate(player, {new})
        room:setPlayerMark(player, "rfenghou_3d15j__suchou_tar", new)
        room:setPlayerMark(player, "@rfenghou_3d15j__suchou", new == 0 and 0 or room:getPlayerById(new).general)
      end
    end
  end,
})

suchou:addEffect(fk.Damaged, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(suchou.name) or target ~= player then return false end
    local mark = player:getMark("rfenghou_3d15j__suchou_tar")
    mark = player.room:getPlayerById(mark)
    return mark and not mark.dead and not player:isNude()
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local cards = room:askToCards(player, {
      min_num = 1,
      max_num = #player:getCardIds("he"),
      include_equip = true,
      skill_name = suchou.name,
      cancelable = true,
      prompt = "#rfenghou_3d15j__suchou-recast"
    })
    if #cards > 0 then
      event:setCostData(self, {cards = cards})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local dat = event:getCostData(self)
    room:recastCard(dat.cards, player, suchou.name)
  end,
})

Fk:loadTranslationTable{
  ["rfenghou_3d15j__suchou"] = "夙雠",
  [":rfenghou_3d15j__suchou"] = "你对一名角色连续造成：两次伤害后，回复1点体力；三次伤害时，可以将之改为致命伤害并失去此技能。若有满足前者的角色存活，你受到伤害后可以重铸任意张牌。",
  ["@rfenghou_3d15j__suchou"] = "夙雠",
  ["#rfenghou_3d15j__suchou-damage"] = "夙雠：你可以将对 %src 造成伤害改为 %arg 点，并失去“夙雠”",
  ["#rfenghou_3d15j__suchou-recast"] = "夙雠：你可以重铸任意张牌",

  ["$rfenghou_3d15j__suchou1"] = "遗身如浮烟，未雪何由安。",
  ["$rfenghou_3d15j__suchou2"] = "父母之仇，弗与共天地！",
}

return suchou
